package cn.codinglives.demo.order.controller;

import cn.codinglives.demo.order.remote.LogisticService;
import cn.codinglives.demo.order.service.OrderService;
import cn.codinglives.demo.order.remote.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class OrderController {
    private final static Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Resource
    private ProductService productService;

    @Resource
    private LogisticService logisticService;

    @Resource
    private OrderService orderService;

    @PostMapping("/order/create")
    public String createOrder(@RequestParam String userId, @RequestParam String productId) {
        logger.info("create order: uid:{} ,/product:{}", userId, productId);
        productService.deductStock(productId);
        String orderId = orderService.createOrder(userId, productId);
        logisticService.delivery(orderId);
        return "ok";
    }
}
